/*     */ package com.zimbra.cs.service.account;
/*     */ 
/*     */ import com.google.common.collect.Lists;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AccountConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.cs.account.AccessManager;
/*     */ import com.zimbra.cs.account.Entry;
/*     */ import com.zimbra.cs.account.NamedEntry;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.accesscontrol.RightManager;
/*     */ import com.zimbra.cs.account.accesscontrol.TargetType;
/*     */ import com.zimbra.cs.account.accesscontrol.UserRight;
/*     */ import com.zimbra.cs.util.AccountUtil;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import com.zimbra.soap.type.TargetBy;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CheckRights
/*     */   extends AccountDocumentHandler
/*     */ {
/*     */   private static class RequestedTarget
/*     */   {
/*     */     private Entry targetEntry;
/*     */     private TargetType targetType;
/*     */     private TargetBy targetBy;
/*     */     private String targetKey;
/*  44 */     private List<UserRight> rights = Lists.newArrayList();
/*     */     
/*     */     private RequestedTarget(Entry targetEntry, TargetType targetType, TargetBy targetBy, String targetKey)
/*     */     {
/*  48 */       this.targetEntry = targetEntry;
/*  49 */       this.targetType = targetType;
/*  50 */       this.targetBy = targetBy;
/*  51 */       this.targetKey = targetKey;
/*     */     }
/*     */     
/*     */     private void addRight(UserRight right) {
/*  55 */       this.rights.add(right);
/*     */     }
/*     */     
/*     */     private Entry getTargetEntry() {
/*  59 */       return this.targetEntry;
/*     */     }
/*     */     
/*     */     private TargetType getTargetType() {
/*  63 */       return this.targetType;
/*     */     }
/*     */     
/*     */     private TargetBy getTargetBy() {
/*  67 */       return this.targetBy;
/*     */     }
/*     */     
/*     */     private String getTargetKey() {
/*  71 */       return this.targetKey;
/*     */     }
/*     */     
/*     */     private List<UserRight> getRights() {
/*  75 */       return this.rights;
/*     */     }
/*     */   }
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  82 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  83 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/*  85 */     List<RequestedTarget> requestedTargets = Lists.newArrayList();
/*     */     
/*  87 */     for (Element eTarget : request.listElements("target")) {
/*  88 */       TargetType targetType = TargetType.fromCode(eTarget.getAttribute("type"));
/*  89 */       TargetBy targetBy = TargetBy.fromString(eTarget.getAttribute("by"));
/*  90 */       String key = eTarget.getAttribute("key");
/*     */       
/*  92 */       Entry entry = findEntry(prov, targetType, targetBy, key);
/*     */       
/*  94 */       RequestedTarget target = new RequestedTarget(entry, targetType, targetBy, key, null);
/*  95 */       requestedTargets.add(target);
/*  96 */       for (Element eRight : eTarget.listElements("right"))
/*     */       {
/*  98 */         target.addRight(RightManager.getInstance().getUserRight(eRight.getText()));
/*     */       }
/*     */       
/* 101 */       if (target.getRights().size() == 0) {
/* 102 */         throw ServiceException.INVALID_REQUEST("missing right for target: " + key, null);
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 107 */     Element response = zsc.createElement(AccountConstants.CHECK_RIGHTS_RESPONSE);
/*     */     
/* 109 */     AccessManager accessMgr = AccessManager.getInstance();
/* 110 */     for (RequestedTarget target : requestedTargets) {
/* 111 */       Entry targetEntry = target.getTargetEntry();
/*     */       
/* 113 */       Element eTarget = response.addElement("target");
/* 114 */       eTarget.addAttribute("type", target.getTargetType().getCode());
/* 115 */       eTarget.addAttribute("by", target.getTargetBy().name());
/* 116 */       eTarget.addAttribute("key", target.getTargetKey());
/*     */       
/* 118 */       boolean combinedResult = true;
/*     */       
/* 120 */       for (UserRight right : target.getRights()) {
/* 121 */         boolean allow = accessMgr.canDo(zsc.getAuthToken(), targetEntry, right, false);
/*     */         
/* 123 */         if ((allow) && (DiscoverRights.isDelegatedSendRight(right)) && (TargetBy.name == target.getTargetBy()))
/*     */         {
/*     */ 
/* 126 */           allow = AccountUtil.isAllowedSendAddress((NamedEntry)targetEntry, target.getTargetKey());
/*     */         }
/* 128 */         eTarget.addElement("right").addAttribute("allow", allow).setText(right.getName());
/*     */         
/*     */ 
/* 131 */         combinedResult &= allow;
/*     */       }
/* 133 */       eTarget.addAttribute("allow", combinedResult);
/*     */     }
/* 135 */     return response;
/*     */   }
/*     */   
/*     */   private Entry findEntry(Provisioning prov, TargetType targetType, TargetBy targetBy, String key) throws ServiceException
/*     */   {
/* 140 */     Entry entry = null;
/* 141 */     switch (targetType) {
/*     */     case account: 
/*     */     case calresource: 
/*     */     case dl: 
/*     */     case group: 
/*     */     case domain: 
/* 147 */       entry = TargetType.lookupTarget(prov, targetType, targetBy, key, false);
/* 148 */       break;
/*     */     default: 
/* 150 */       throw ServiceException.INVALID_REQUEST("unsupported target type: " + targetType.getCode(), null);
/*     */     }
/*     */     
/*     */     
/* 154 */     if ((entry == null) && (TargetBy.id == targetBy)) {
/* 155 */       throw ServiceException.INVALID_REQUEST("no such entry: " + key, null);
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 162 */     return entry;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/account/CheckRights.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */